home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Toolbox / icon cache demo / icon cache.c next >
Encoding:
Text File  |  1996-01-25  |  4.6 KB  |  219 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    This sample demonstrates the use of an icon cache
  3.     //    to limit the search for icon resource to one resource
  4.     //    file. It does this by installing an icon getter function
  5.     //    into the cache which calls Get1(Ind)Resource instead of the
  6.     //    usual GetResource.
  7.     //
  8.     //    The application is meant to display the first
  9.     //    suite produced by an indexed resource call. There's nothing
  10.     //    stopping you from calling Get1Resource or anything else
  11.     //    which might produce a handle to a member of an icon suite.
  12.     //
  13.     //    There's also some jiggery-pokery having to do with my
  14.     //    distaste for purgeable handles; see below for details.
  15.     //
  16.     //    Complaints and kudos to:
  17.     //
  18.     //        Pete Gontier
  19.     //        Apple Macintosh Developer Technical Support
  20.     //        <gurgle@apple.com>
  21.     //
  22.  
  23. #define OLDROUTINELOCATIONS        0
  24. #define OLDROUTINENAMES            0
  25. #define SystemSevenOrLater        1
  26.  
  27. #ifndef __FONTS__
  28. #    include <Fonts.h>
  29. #endif
  30.  
  31. #ifndef __DIALOGS__
  32. #    include <Dialogs.h>
  33. #endif
  34.  
  35. #ifndef __RESOURCES__
  36. #    include <Resources.h>
  37. #endif
  38.  
  39. #ifndef __STANDARDFILE__
  40. #    include <StandardFile.h>
  41. #endif
  42.  
  43. #ifndef __ICONS__
  44. #    include <Icons.h>
  45. #endif
  46.  
  47. static pascal Handle MyIconGetter (ResType rt, void *)
  48. {
  49.     //
  50.     //    DisposeIconSuite assumes resources are purgeable and doesn't
  51.     //    release them. Since we would rather the memory be freed,
  52.     //    we have our icon getter function ('MyIconGetter') detach
  53.     //    each resource it gets. This turns off the resource bit for
  54.     //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  55.     //
  56.  
  57.     Handle iconH = Get1IndResource (rt,1);
  58.     if (!iconH || ResError ( ))
  59.         return nil;
  60.  
  61.     DetachResource (iconH);
  62.     if (ResError ( ))
  63.     {
  64.         ReleaseResource (iconH);
  65.         return nil;
  66.     }
  67.  
  68.     return iconH;
  69. }
  70.  
  71. static pascal void Plot1IndIconSuite (Rect *rect)
  72. {
  73.     Handle iconCacheH;
  74.  
  75.     if (!MakeIconCache (&iconCacheH,MyIconGetter,nil))
  76.     {
  77.         PlotIconSuite (rect,kAlignNone,kTransformNone,iconCacheH);
  78.  
  79.         //
  80.         //    DisposeIconSuite assumes resources are purgeable and doesn't
  81.         //    release them. Since we would rather the memory be freed,
  82.         //    we have our icon getter function ('MyIconGetter') detach
  83.         //    each resource it gets. This turns off the resource bit for
  84.         //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  85.         //
  86.  
  87.         DisposeIconSuite (iconCacheH,true);
  88.     }
  89. }
  90.  
  91. //////////////////////////////////////////////////////////////////////
  92. //
  93. //    Below please find the usual sort of application boilerplate.
  94. //
  95. //////////////////////////////////////////////////////////////////////
  96.  
  97. static Boolean gQuitting;
  98.  
  99. static pascal OSErr InitMac (void)
  100. {
  101.     MaxApplZone ( );
  102.     InitGraf (&(qd.thePort));
  103.     InitFonts ( );
  104.     InitWindows ( );
  105.     InitMenus ( );
  106.     TEInit ( );
  107.     InitDialogs (nil);
  108.  
  109.     return noErr;
  110. }
  111.  
  112. static pascal void IconUserItem (WindowRef dlgRef, short itemNo)
  113. {
  114.     short crf = CurResFile ( );
  115.     short iType; Handle iHandle; Rect iRect;
  116.     GetDialogItem (dlgRef,itemNo,&iType,&iHandle,&iRect);
  117.     UseResFile (GetWRefCon (dlgRef));
  118.     if (!ResError ( ))
  119.     {
  120.         Plot1IndIconSuite (&iRect);
  121.         UseResFile (crf);
  122.     }
  123. }
  124.  
  125. static pascal void GrabIcon (void)
  126. {
  127.     StandardFileReply sfr;
  128.     StandardGetFile (nil,-1,nil,&sfr);
  129.     if (sfr.sfGood)
  130.     {
  131.         short crf = CurResFile ( );
  132.         short resRefNum = FSpOpenResFile (&(sfr.sfFile),fsRdPerm);
  133.         if (resRefNum != -1)
  134.         {
  135.             DialogRef dlgRef = nil;
  136.  
  137.             UseResFile (crf);
  138.  
  139.             dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
  140.             if (dlgRef)
  141.             {
  142.                 short itemHit;
  143.  
  144.                 short iType; Handle iHandle; Rect iRect;
  145.  
  146.                 GetDialogItem (dlgRef,2,&iType,&iHandle,&iRect);
  147.                 SetDialogItem (dlgRef,2,iType,(Handle)IconUserItem,&iRect);
  148.                 SetWRefCon (dlgRef,resRefNum);
  149.  
  150.                 ShowWindow (dlgRef);
  151.                 ModalDialog (nil,&itemHit);
  152.  
  153.                 DisposeDialog (dlgRef);
  154.             }
  155.             
  156.             CloseResFile (resRefNum);
  157.         }
  158.     }
  159. }
  160.  
  161. static pascal void Command (long ms)
  162. {
  163.     short    menuID = ms >> 16,
  164.             menuItem = ms;
  165.  
  166.     if (menuID == 129)
  167.     {
  168.         if (menuItem == 2)
  169.             gQuitting = true;
  170.         else if (menuItem == 1)
  171.             GrabIcon ( );
  172.     }
  173. }
  174.  
  175. static pascal void HandleEvent (const EventRecord *eventP)
  176. {
  177.     if (eventP->what == mouseDown)
  178.     {
  179.         WindowRef whichWindow;
  180.         short partCode = FindWindow (eventP->where, &whichWindow);
  181.         if (partCode == inMenuBar)
  182.         {
  183.             long ms = MenuSelect (eventP->where);
  184.             if (ms) Command (ms);
  185.             HiliteMenu (0);
  186.         }
  187.     }
  188. }
  189.  
  190. static pascal Boolean SetUpMenuBar (void)
  191. {
  192.     Boolean result = false;
  193.     Handle mBar = GetNewMBar (128);
  194.     if (!ResError ( ) && mBar)
  195.     {
  196.         SetMenuBar (mBar);
  197.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  198.         DrawMenuBar ( );
  199.         result = true;
  200.         ReleaseResource (mBar);
  201.     }
  202.     return result;
  203. }
  204.  
  205. void main (void)
  206. {
  207.     if (!InitMac ( ) && SetUpMenuBar ( ))
  208.     {    
  209.         do
  210.         {
  211.             EventRecord event;
  212.             InitCursor ( );
  213.             WaitNextEvent (everyEvent, &event, -1, nil);
  214.             HandleEvent (&event);
  215.         }
  216.         while (!gQuitting);
  217.     }
  218. }
  219.